#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeachReachRcdProvider : CY.GFive.Core.Providers.DALProvider.ITeachReachRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectTeachReachRcd = "SELECT [Id], [TRCode],[StartTime],[EndTime],[ConsistTime],[Partners],[Subject],[Des],[AttachmentCode],[YearNum],[Term] FROM [TeachReachRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeachReachRcd = "INSERT INTO [TeachReachRcd]([TRCode],[StartTime],[EndTime],[ConsistTime],[Partners],[Subject],[Des],[AttachmentCode],[YearNum],[Term]) values(@TRCode,@StartTime,@EndTime,@ConsistTime,@Partners,@Subject,@Des,@AttachmentCode,@YearNum,@Term);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeachReachRcd = "UPDATE [TeachReachRcd] SET [TRCode] = @TRCode,[StartTime] = @StartTime,[EndTime] = @EndTime,[ConsistTime] = @ConsistTime,[Partners] = @Partners,[Subject] = @Subject,[Des] = @Des,[AttachmentCode] = @AttachmentCode,[YearNum] = @YearNum,[Term] = @Term WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeachReachRcd = "DELETE [TeachReachRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeachReachRcd = "SELECT [Id], [TRCode],[StartTime],[EndTime],[ConsistTime],[Partners],[Subject],[Des],[AttachmentCode],[YearNum],[Term] FROM [TeachReachRcd]";
		#endregion

		#region ITeachReachRcdProvider Members
		public Core.Business.TeachReachRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeachReachRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeachReachRcd teachReachRcd = new Core.Business.TeachReachRcd();

				if (!reader.IsDBNull(0)) teachReachRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teachReachRcd.TRCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) teachReachRcd.StartTime = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) teachReachRcd.EndTime = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) teachReachRcd.ConsistTime = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) teachReachRcd.Partners = reader.GetString(5);
				if (!reader.IsDBNull(6)) teachReachRcd.Subject = reader.GetString(6);
				if (!reader.IsDBNull(7)) teachReachRcd.Des = reader.GetString(7);
				if (!reader.IsDBNull(8)) teachReachRcd.AttachmentCode = reader.GetString(8);
				if (!reader.IsDBNull(9)) teachReachRcd.YearNum = reader.GetString(9);
				if (!reader.IsDBNull(10)) teachReachRcd.Term = reader.GetInt32(10);

				return teachReachRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeachReachRcd teachReachRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TRCode", SqlDbType.NVarChar, teachReachRcd.TRCode);
			sql.AddParameter("@StartTime", SqlDbType.DateTime, teachReachRcd.StartTime);
			sql.AddParameter("@EndTime", SqlDbType.DateTime, teachReachRcd.EndTime);
			sql.AddParameter("@ConsistTime", SqlDbType.DateTime, teachReachRcd.ConsistTime);
			sql.AddParameter("@Partners", SqlDbType.NVarChar, teachReachRcd.Partners);
			sql.AddParameter("@Subject", SqlDbType.NVarChar, teachReachRcd.Subject);
			sql.AddParameter("@Des", SqlDbType.NVarChar, teachReachRcd.Des);
			sql.AddParameter("@AttachmentCode", SqlDbType.NVarChar, teachReachRcd.AttachmentCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, teachReachRcd.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, teachReachRcd.Term);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeachReachRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teachReachRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeachReachRcd teachReachRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TRCode", SqlDbType.NVarChar, teachReachRcd.TRCode);
			sql.AddParameter("@StartTime", SqlDbType.DateTime, teachReachRcd.StartTime);
			sql.AddParameter("@EndTime", SqlDbType.DateTime, teachReachRcd.EndTime);
			sql.AddParameter("@ConsistTime", SqlDbType.DateTime, teachReachRcd.ConsistTime);
			sql.AddParameter("@Partners", SqlDbType.NVarChar, teachReachRcd.Partners);
			sql.AddParameter("@Subject", SqlDbType.NVarChar, teachReachRcd.Subject);
			sql.AddParameter("@Des", SqlDbType.NVarChar, teachReachRcd.Des);
			sql.AddParameter("@AttachmentCode", SqlDbType.NVarChar, teachReachRcd.AttachmentCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, teachReachRcd.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, teachReachRcd.Term);
			sql.AddParameter("@Id", SqlDbType.Int, teachReachRcd.Id);
			sql.ExecuteSql(SqlUpdateTeachReachRcd);
		}

		public void Delete(Core.Business.TeachReachRcd teachReachRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teachReachRcd.Id);
			sql.ExecuteSql(SqlDeleteTeachReachRcd);
		}

		public IList<Core.Business.TeachReachRcd> GetAllTeachReachRcd()
		{
			IList<Core.Business.TeachReachRcd> teachReachRcdlist = new List<Core.Business.TeachReachRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeachReachRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeachReachRcd teachReachRcd = new Core.Business.TeachReachRcd();

					if (!reader.IsDBNull(0)) teachReachRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teachReachRcd.TRCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) teachReachRcd.StartTime = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) teachReachRcd.EndTime = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) teachReachRcd.ConsistTime = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) teachReachRcd.Partners = reader.GetString(5);
					if (!reader.IsDBNull(6)) teachReachRcd.Subject = reader.GetString(6);
					if (!reader.IsDBNull(7)) teachReachRcd.Des = reader.GetString(7);
					if (!reader.IsDBNull(8)) teachReachRcd.AttachmentCode = reader.GetString(8);
					if (!reader.IsDBNull(9)) teachReachRcd.YearNum = reader.GetString(9);
					if (!reader.IsDBNull(10)) teachReachRcd.Term = reader.GetInt32(10);

					teachReachRcd.MarkOld();
					teachReachRcdlist.Add(teachReachRcd);
				}
				reader.Close();
			}
			return teachReachRcdlist;
		}
		#endregion
	}
}
